import nme.Assets;
import nme.display.Graphics;
import nme.display.DisplayObject;
import nme.display.Sprite;
import nme.display.Bitmap;
import nme.display.BitmapData;

import nme.geom.Rectangle;
import nme.geom.Matrix;
import nme.geom.Point;

import nme.events.Event;
import nme.events.MouseEvent;
import nme.events.KeyboardEvent;
import nme.Lib;

enum ModuleAnchor {
	NONE;
	LEFT_TOP;	LEFT_CENTER;	LEFT_BOTTOM;
	CENTER_TOP;	CENTER_CENTER;	CENTER_BOTTOM;
	RIGHT_TOP;	RIGHT_CENTER;	RIGHT_BOTTOM;
}

class ModuleSelection extends Sprite
{
	public var rect (default, null):Rectangle;
	public var control (default, null):Sprite;
	
	private var left_top:Sprite;
	private var left_center:Sprite;
	private var left_bottom:Sprite;
	private var center_top:Sprite;
	private var center_center:Sprite;
	private var center_bottom:Sprite;
	private var right_top:Sprite;
	private var right_center:Sprite;
	private var right_bottom:Sprite;
	
	private var anchor:ModuleAnchor;
	private var downPoint:Point;
	
	public var enable:Bool = true;
	
	public function new (_x:Float, _y:Float, _width:Float, _height:Float)
	{
		super ();
		rect = new Rectangle (_x, _y, _width, _height);
		
		addEventListener (MouseEvent.MOUSE_DOWN, onMouseDown);
		addEventListener (MouseEvent.MOUSE_UP, onMouseUp);
		addEventListener (MouseEvent.ROLL_OUT, onMouseUp);
		addEventListener (MouseEvent.MOUSE_MOVE, onMouseMove);
		
		anchor = ModuleAnchor.NONE;
		downPoint = new Point ();
		
		control	= new Sprite ();
		
		addChild (left_top		= new Sprite ());
		addChild (left_center	= new Sprite ());
		addChild (left_bottom	= new Sprite ());
		addChild (center_top	= new Sprite ());
		addChild (center_center	= new Sprite ());
		addChild (center_bottom	= new Sprite ());
		addChild (right_top		= new Sprite ());
		addChild (right_center	= new Sprite ());
		addChild (right_bottom	= new Sprite ());
		
		for (childId in 0 ... numChildren)
		{
			var child:DisplayObject = getChildAt (childId);
			child.graphics.clear ();
			child.graphics.beginFill (0x000000, 1);
			child.graphics.drawRect (-2, -2, 4, 4);
			child.graphics.endFill ();
			child.alpha = 0;
		}
		
		updatePosition ();
	}

	private function onkeyboard (e:KeyboardEvent):Void
	{
	}
	
	private function onMouseDown (e:MouseEvent):Void
	{
		if (!enable) return;
		downPoint.x = e.stageX;
		downPoint.y = e.stageY;
		var offset:Int = 10;
		if (Math.abs (e.stageX - rect.left) < offset)
		{
			if (Math.abs (e.stageY - rect.top) < offset)			anchor = ModuleAnchor.LEFT_TOP;
			else if (Math.abs (e.stageY - rect.bottom) < offset)	anchor = ModuleAnchor.LEFT_BOTTOM;
			else													anchor = ModuleAnchor.LEFT_CENTER;
		}
		else if (Math.abs (e.stageX - rect.right) < offset)
		{
			if (Math.abs (e.stageY - rect.top) < offset)			anchor = ModuleAnchor.RIGHT_TOP;
			else if (Math.abs (e.stageY - rect.bottom) < offset)	anchor = ModuleAnchor.RIGHT_BOTTOM;
			else													anchor = ModuleAnchor.RIGHT_CENTER;
		}
		else
		{
			if (Math.abs (e.stageY - rect.top) < offset)			anchor = ModuleAnchor.CENTER_TOP;
			else if (Math.abs (e.stageY - rect.bottom) < offset)	anchor = ModuleAnchor.CENTER_BOTTOM;
			else													anchor = ModuleAnchor.CENTER_CENTER;
		}
		switch (anchor)
		{
			case NONE:
			case LEFT_TOP:			left_top.alpha		= 1;
			case LEFT_CENTER:		left_center.alpha	= 1;
			case LEFT_BOTTOM:		left_bottom.alpha	= 1;
			case CENTER_TOP:		center_top.alpha	= 1;
			case CENTER_CENTER:		center_center.alpha	= 1;
			case CENTER_BOTTOM:		center_bottom.alpha	= 1;
			case RIGHT_TOP:			right_top.alpha		= 1;
			case RIGHT_CENTER:		right_center.alpha	= 1;
			case RIGHT_BOTTOM:		right_bottom.alpha	= 1;
		}
	}
	
	private function onMouseUp (e:MouseEvent):Void
	{
		if (!enable) return;
		switch (anchor)
		{
			case NONE:
			case LEFT_TOP:			left_top.alpha		= 0;
			case LEFT_CENTER:		left_center.alpha	= 0;
			case LEFT_BOTTOM:		left_bottom.alpha	= 0;
			case CENTER_TOP:		center_top.alpha	= 0;
			case CENTER_CENTER:		center_center.alpha	= 0;
			case CENTER_BOTTOM:		center_bottom.alpha	= 0;
			case RIGHT_TOP:			right_top.alpha		= 0;
			case RIGHT_CENTER:		right_center.alpha	= 0;
			case RIGHT_BOTTOM:		right_bottom.alpha	= 0;
		}
		anchor = ModuleAnchor.NONE;
	}
	
	private function onMouseMove (e:MouseEvent):Void
	{
		if (!enable) return;
		if (anchor == ModuleAnchor.NONE) return;
		
		var offsetX:Float = e.stageX - downPoint.x;
		var offsetY:Float = e.stageY - downPoint.y;
		switch (anchor)
		{
			case NONE:
			case LEFT_TOP:			rect.left	+= offsetX;		rect.top	+= offsetY;
			case LEFT_CENTER:		rect.left	+= offsetX;	
			case LEFT_BOTTOM:		rect.left	+= offsetX;		rect.bottom += offsetY;
			case CENTER_TOP:									rect.top	+= offsetY;
			case CENTER_CENTER:		rect.x		+= offsetX;		rect.y		+= offsetY;
			case CENTER_BOTTOM:		rect.bottom	+= offsetY;	
			case RIGHT_TOP:			rect.right	+= offsetX;		rect.top	+= offsetY;
			case RIGHT_CENTER:		rect.right	+= offsetX;	
			case RIGHT_BOTTOM:		rect.right	+= offsetX;		rect.bottom	+= offsetY;
		}
		downPoint.x = e.stageX;
		downPoint.y = e.stageY;
		updatePosition ();
	}
	
	private function updatePosition ():Void
	{
		this.x				= rect.x;				this.y		= rect.y;		
		left_top.x			= 0;					left_top.y		= 0;
		left_center.x		= 0;					left_center.y	= rect.height * 0.5;
		left_bottom.x		= 0;					left_bottom.y	= rect.height;
		center_top.x		= rect.width * 0.5;		center_top.y	= 0;
		center_center.x		= rect.width * 0.5;		center_center.y	= rect.height * 0.5;
		center_bottom.x		= rect.width * 0.5;		center_bottom.y	= rect.height;
		right_top.x			= rect.width;			right_top.y		= 0;
		right_center.x		= rect.width;			right_center.y	= rect.height * 0.5;
		right_bottom.x		= rect.width;			right_bottom.y	= rect.height;
		
		redraw ();
	}
	
	private function redraw ():Void
	{
		graphics.clear ();
		graphics.beginFill (0x000000, 0.005);
		graphics.drawRect (0 - 10, 0 - 10, rect.width + 20, rect.height + 20);
		graphics.lineStyle (1, 0x000000, 0.8);
		graphics.drawRect (0, 0, rect.width, rect.height);
		graphics.endFill ();
		
		control.graphics.clear ();
		control.graphics.beginFill (0x000000, 1.0);
		control.graphics.drawRect (rect.x, rect.y, rect.width, rect.height);
		control.graphics.endFill ();
	}
	
	public function translate (dx:Float, dy:Float):Void
	{
		rect.x += dx;
		rect.y += dy;
		
		this.x += dx;
		this.y += dy;
	}
}